<?php
    class Group_Model extends CI_Model{
        
        function __construct() {
          parent::__construct();
        }      
        function get_all_group(){
          return $this->db->get('admin_group')->result();
        }

        function get_group_by_id($id){
          $this->db->where('group_id',$id);
          return $this->db->get('admin_group')->row();
        }
      
        function save($group_id,$data){          
            if($group_id!=0){
              $this->db->where('group_id',$group_id);
              if($this->db->update('admin_group',$data)){
                  return true;
              }else{
                  return false;
              }
            }else{
              if($this->db->insert('admin_group',$data)){
                  return $this->db->insert_id();
              }else{
                  return false;
              }
            }
        }
        function delete($id){
            $this->db->where('group_id',$id);
            $this->db->delete('admin_right');
            
            $this->db->where('group_id',$id);
            $query = $this->db->delete('admin_group');
            if($query){
                return true;
            }else{
                return false;
            }
            
        }
        function get_all_module(){
          return $this->db->get('admin_module')->result();
        }
        function get_all_function($id_module){
            $this->db->where('module_id',$id_module);
            return $this->db->get('admin_function')->result();
        }
        function get_right_gr_md_fc($group_id,$module_id,$function_id){
            $this->db->where('group_id',$group_id);
            $this->db->where('module_id',$module_id);
            $this->db->where('function_id',$function_id);
            return $this->db->get('admin_right')->row();
        }
        function save_right($right_id,$data){          
            if($right_id>0){
              $this->db->where('right_id',$right_id);
              if($this->db->update('admin_right',$data)){
                  return true;
              }else{
                  return false;
              }
            }else{
              if($this->db->insert('admin_right',$data)){
                  return true;
              }else{
                  return false;
              }
            }
        }
        function getAllPermission(){
            return $this->db->get('admin_permission')->result();
        }
  }
?>
